package com.kkb.cubemall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kkb.cubemall.common.utils.PageUtils;
import com.kkb.cubemall.common.utils.Query;
import com.kkb.cubemall.product.dao.*;
import com.kkb.cubemall.product.entity.*;
import com.kkb.cubemall.product.service.SpuInfoService;
import com.kkb.cubemall.product.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.MethodArgumentNotValidException;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @Autowired
    private SpuInfoDescDao spuInfoDescDaop;

    @Autowired
    private SpuImagesDao spuImagesDao;

    @Autowired
    private ProductAttrValueDao productAttrValueDao;

    @Autowired
    private AttrDao attrDao;

    @Autowired
    private SkuInfoDao skuInfoDao;

    @Autowired
    private SkuImagesDao skuImagesDao;

    @Autowired
    private SkuSaleAttrValueDao skuSaleAttrValueDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = MethodArgumentNotValidException.class)
    public void saveSpuInfo(SpuSaveVo spuSaveVo) {
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(spuSaveVo, spuInfoEntity);
        spuInfoEntity.setCreateTime(new Date());
        spuInfoEntity.setUpdateTime(new Date());
        this.save(spuInfoEntity);

        SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity();
        spuInfoDescEntity.setSpuId(spuInfoEntity.getId());
        spuInfoDescEntity.setDecript(spuSaveVo.getDecript());
        spuInfoDescDaop.insert(spuInfoDescEntity);

        SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
        spuImagesEntity.setSpuId(spuInfoEntity.getId());
        List<String> images = spuSaveVo.getImages();
        for (String image : images) {
            spuImagesEntity.setImgUrl(image);
            spuImagesDao.insert(spuImagesEntity);
        }

        List<BaseAttrs> baseAttrs = spuSaveVo.getBaseAttrs();
        ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
        productAttrValueEntity.setSpuId(spuInfoEntity.getId());
        for (BaseAttrs attrs : baseAttrs) {
            productAttrValueEntity.setAttrId(attrs.getAttrId());
            AttrEntity attrEntity = attrDao.selectById(attrs.getAttrId());
            if (attrEntity != null) {
                productAttrValueEntity.setAttrName(attrEntity.getName());
                productAttrValueEntity.setAttrValue(attrs.getAttrValues());
                productAttrValueEntity.setQuickShow(attrs.getShowDesc());
                productAttrValueDao.insert(productAttrValueEntity);
            }
        }

        List<Skus> skusList = spuSaveVo.getSkus();
        skusList.forEach(skus -> {
            SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
            skuInfoEntity.setSpuId(spuInfoEntity.getId());
            skuInfoEntity.setSkuName(skus.getSkuName());
            skuInfoEntity.setCategoryId(spuInfoEntity.getCategoryId());
            skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());
            skuInfoEntity.setSkuTitle(skus.getSkuTitle());
            skuInfoEntity.setSkuSubtitle(skus.getSkuSubtitle());
            skuInfoEntity.setPrice(skus.getPrice());
            skuInfoEntity.setSaleCount(0L);
            String defaultImage = null;
            for (Images skuImage : skus.getImages()) {
                if (skuImage.getDefaultImg() == 1) {
                    defaultImage = skuImage.getImgUrl();
                }
            }
            skuInfoEntity.setSkuDefaultImg(defaultImage);
            skuInfoDao.insert(skuInfoEntity);

            List<Images> imagesList = skus.getImages();
            for (Images skuImage : imagesList) {
                SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                skuImagesEntity.setSkuId(skuInfoEntity.getId());
                skuImagesEntity.setImgUrl(skuImage.getImgUrl());
                skuImagesEntity.setDefaultImg(skuImage.getDefaultImg());
                skuImagesDao.insert(skuImagesEntity);
            }

            List<Attr> attrList = skus.getAttr();
            for (Attr attr : attrList) {
                SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
                skuSaleAttrValueEntity.setSkuId(skuInfoEntity.getId());
                skuSaleAttrValueEntity.setAttrId(attr.getAttrId());
                skuSaleAttrValueEntity.setAttrName(attr.getAttrName());
                skuSaleAttrValueEntity.setAttrValue(attr.getAttrValue());
                skuSaleAttrValueDao.insert(skuSaleAttrValueEntity);
            }
        });
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<SpuInfoEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and(qw -> {
                qw.eq("id", key).or().like("spu_name", key);
            });
        }

        String categoryId = (String) params.get("categoryId");
        if (!StringUtils.isEmpty(categoryId) && !"0".equals(categoryId)) {
            queryWrapper.eq("category_id", categoryId);
        }
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId) && !"0".equals(brandId)) {
            queryWrapper.eq("brand_id", brandId);
        }
        String publishStatus = (String) params.get("status");
        if (!StringUtils.isEmpty(publishStatus)) {
            queryWrapper.eq("publish_status", publishStatus);
        }

        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

}